#include "Trapeze.h"
#include <cmath>

inline int Trapeze::vectorMult (POINT firstVector, POINT secondVector)
{
	return (firstVector.x*secondVector.y - firstVector.y*secondVector.x);
}

bool Trapeze::isParallel (POINT firstVector, POINT secondVector)			
{
	int mult = vectorMult(firstVector, secondVector);
	return !mult;
}

bool Trapeze::isTrapeze (POINT figure[])
{
	const char M = 2;
	POINT vector[M];
	for (char i = 0, k = N - 1; i < M; i++, k--)
	{
		vector[i].x = figure[k].x - figure[i].x;
		vector[i].y = figure[k].y - figure[i].y;
	}
	if (isParallel(vector[0], vector[1]))
	{
		for (char i = 0, k = 0; i < M; i++, k += 3)
		{
			vector[i].x = figure[i + 1].x - figure[k].x;
			vector[i].y = figure[i + 1].y - figure[k].y;
		}
		if (!isParallel(vector[0], vector[1]))
			return true;
	}
	return false;
}

bool Trapeze::isVisible (HDC hdc, HWND hwnd, POINT figure[])
{
	RECT size;
	GetClientRect(hwnd, &size);
	for (char i = 0; i < N; i++)
		if (!((figure[i].x < size.right) && (figure[i].y < size.bottom) && (figure[i].x >= 0) && (figure[i].y >= 0)))
			return false;
	return true;
}

void Trapeze::setPoints (POINT newPoints[])
{
	for (int i = 0; i < N; i++)
		points[i] = newPoints[i];
}

void Trapeze::setWidth (int newWidth)
{	width = newWidth;}

void Trapeze::setLineColor (COLORREF newLineColor)
{	lineColor = newLineColor;}

void Trapeze::setFloodColor (COLORREF newFloodColor)
{	floodColor = newFloodColor;}

void Trapeze::setStyle (int newStyle)
{	style = newStyle;}

void Trapeze::getPoints (POINT newPoints[])
{
	for (int i = 0; i < N; i++)
		*(newPoints + i) = points[i];
}

int Trapeze::getWidth ()
{	return width;}

int Trapeze::getStyle ()
{	return style;}

COLORREF Trapeze::getLineColor ()
{	return lineColor;}

COLORREF Trapeze::getFloodColor ()
{	return floodColor;}

void Trapeze::drawFigure (HWND hwnd, HDC hdc)
{
	POINT currentPoints[N];
	getPoints(currentPoints);
	if (isTrapeze(currentPoints)) 
		Polygon(hdc, currentPoints, N);
	else
		throw NotTrapeze;
}

void Trapeze::moveUp (HWND hwnd, HDC hdc)
{
	POINT newPoints[N];
	getPoints(newPoints);
	for (char i = 0; i < N; i++)
		newPoints[i].y--;
	if (isVisible(hdc, hwnd, newPoints))
		setPoints(newPoints);
	else
		throw NotVisible;
}

void Trapeze::moveDown (HWND hwnd, HDC hdc)
{
	POINT newPoints[N];
	getPoints(newPoints);
	for (char i = 0; i < N; i++)
		newPoints[i].y++;
	if (isVisible(hdc, hwnd, newPoints))
		setPoints(newPoints);
	else
		throw NotVisible;
}

void Trapeze::moveRight (HWND hwnd, HDC hdc)
{
	POINT newPoints[N];
	getPoints(newPoints);
	for (char i = 0; i < N; i++)
		newPoints[i].x++;
	if (isVisible(hdc, hwnd, newPoints))
		setPoints(newPoints);
	else
		throw NotVisible;
}

void Trapeze::moveLeft (HWND hwnd, HDC hdc)
{
	POINT newPoints[N];
	getPoints(newPoints);
	for (char i = 0; i < N; i++)
		newPoints[i].x--;
	if (isVisible(hdc, hwnd, newPoints))
		setPoints(newPoints);
	else
		throw NotVisible;
}

/*bool Trapeze::rotate ()
{
	POINT oldPoints[N];
	POINT newPoints[N];
	getPoints(oldPoints);
	for (char i = 0; i < N; i++)
	{
		newPoints[i].x = cos(PI/2)*oldPoints[i].x + sin(PI/2)*oldPoints[i].y;
		newPoints[i].x = sin(PI/2)*oldPoints[i].x + cos(PI/2)*oldPoints[i].y;
	}
	setPoints(newPoints);
	return true;
}*/

